// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
	"shop_id": {
		"rules": [{
			"format": "string"
		}],
		"title": "所属店铺",
		"defaultValue": "yipaidaojia",
		"label": "所属店铺"
	},
	"categories_id": {
		"rules": [
			{
				"required": true,
				"errorMessage": "请选择分类"
			},
			{
			"format": "string"
		}]
	},
	"title": {
		"rules": [
			{
				"required": true,
				"errorMessage": "请输入标题"
			},
			{
				"format": "string"
			}
		]
	},
	"score": {
		"rules": [
		{
			"required": true,
			"errorMessage": "请设置所需积分"
		},
		{
			"format": "int"
		}],
		"defaultValue": null
	},
	"image": {
		
		"rules": [
			{
				"required": true,
				"errorMessage": "请至少上传一张照片"
			},
			{
				"format": "array"
			},
			{
				"arrayType": "file"
			},
			{
				"maxLength": 50
			}
		],
		"title": "商品照片",
		"label": "商品照片"
	},
	"attribute": {
		"rules": [{
			"format": "object"
		}]
	},
	"url": {
		"rules": [{
			"format": "string"
		}]
	},
	"is_shelf": {
		"rules": [{
			"format": "bool"
		}],
		"defaultValue": false
	},
	"sales": {
		"rules": [{
				"format": "int"
			},
			{
				"minimum": 0
			}
		],
		"title": "总销量",
		"defaultValue": 0,
		"label": "总销量"
	},
	"sku": {
		"rules": [{
			"format": "object"
		}]
	}
}

const enumConverter = {}

function filterToWhere(filter, command) {
	let where = {}
	for (let field in filter) {
		let {
			type,
			value
		} = filter[field]
		switch (type) {
			case "search":
				if (typeof value === 'string' && value.length) {
					where[field] = new RegExp(value)
				}
				break;
			case "select":
				if (value.length) {
					let selectValue = []
					for (let s of value) {
						selectValue.push(command.eq(s))
					}
					where[field] = command.or(selectValue)
				}
				break;
			case "range":
				if (value.length) {
					let gt = value[0]
					let lt = value[1]
					where[field] = command.and([command.gte(gt), command.lte(lt)])
				}
				break;
			case "date":
				if (value.length) {
					let [s, e] = value
					let startDate = new Date(s)
					let endDate = new Date(e)
					where[field] = command.and([command.gte(startDate), command.lte(endDate)])
				}
				break;
			case "timestamp":
				if (value.length) {
					let [startDate, endDate] = value
					where[field] = command.and([command.gte(startDate), command.lte(endDate)])
				}
				break;
		}
	}
	return where
}

export {
	validator,
	enumConverter,
	filterToWhere
}